from datetime import datetime, timedelta
def isFloat(val):
    """simple float checker"""
    try:
        float(val)
        return True
    except ValueError:
        return False

def dNum2DTime(date):
    """converts matlab datenum (1 = 0000/01/01 00:00:00) to datetime gregorian
       ordinal (1 = 0001/01/01 00:00:00) and from datetime object to datenum""" 
    try:
    # If date is a float (datenum) then convert to gregorian
        date = date - 366
    # Add fractioned day
        secs = (date - int(date)) * 24 * 60 * 60
    # Create date object
        do = datetime.fromordinal(int(date))
    # Add fractioned day
        do = do + timedelta(seconds = secs)
        return do
    except TypeError:
    # If date is a datetime object then convert to datenum
        return date.toordinal() + date.hour/24. + date.minute/1440.+\
                date.second/86400. + 366

def dTime2Loch(date):
    """converts pre-industrial baseline date (1760/0/1 00:00:00 UT to datetime
       gregorian ordinal and from datetime object to pre-industrial numeral"""
    try:
    # If date is a float (Loch day) then convert to gregorian
        return date + 642462
    except TypeError:
    # If date is a datetime object then convert to pre-industrial baseline
        return date.toordinal() + date.hour/24. + date.minute/1440.+\
                date.second/86400. - 642462
